<template>
  <a-radio-group buttonStyle="solid" @change="handleChange" :value="seletedValue">
    <a-radio-button v-if="dictCategoryOpt" key="dictCategory" value="dictCategory">
      {{ showCode ? '字典分类' : '字典分类' }}
    </a-radio-button>
    <a-radio-button v-for="(item, dictCode) in dictOptions" :key="dictCode" :value="item.dictCode">
      {{ showCode ? item.dictEntry : item.dictEntry }}
    </a-radio-button>
  </a-radio-group>
</template>

<script>
import { getDictByCategory } from '../dict/dict'

export default {
  name: 'DictSelectBtn',
  props: {
    dictCategory: {
      required: true,
      type: String,
    },
    placeholder: {
      default: '',
      required: false,
      type: String,
    },
    dictCategoryOpt: {
      default: false,
      required: false,
      type: Boolean,
    },
    showCode: {
      default: false,
      required: false,
      type: Boolean,
    },
    all: {
      default: false,
      required: false,
      type: Boolean,
    },
    value: {
      default: '',
      required: false,
      type: String,
    },
  },
  data() {
    return {
      dictOptions: [],
      seletedValue: this.value,
    }
  },
  created() {
    this.initDictData()
  },
  methods: {
    initDictData() {
      if (this.dictCategory === '字典分类') {
        this.dictCategory = 'dictCategory'
      }
      const parameter = {
        dictCategory: this.dictCategory,
        sidx: 'dictCode',
        order: '1',
      }
      getDictByCategory(parameter, {}).then((r) => {
        if (r && r && r.code === 'C1' && r.data) {
          this.dictOptions = r.data
        }
        if (this.all) {
          const tn = {
            dictCode: 'all',
            dictEntry: '全部',
          }
          this.dictOptions.unshift(tn)
        }
      })
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    handleChange(val) {
      this.$emit('change', val.target.value)
      // this.$emit('dataChanged', val)
    },
  },
  watch: {
    value: function () {
      this.seletedValue = this.value
    },
  },
}
</script>
